package com.minmin.common;

import javax.mail.*;
import javax.mail.internet.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class JavaMailUtil {
    private void sendEmail(String host, String port,
                           final String userName, final String password, String toAddress,
                           String subject, String emailBody, String attachmentPath) throws
            MessagingException, IOException {
        // 设置SMTP服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        // 新建一个认证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };

        // 新建一个会话
        Session session = Session.getInstance(properties, auth);

        // 新建一个消息，并设置其属性
        MimeMessage message = new MimeMessage(session);
        message.setContent(emailBody, "text/plain");
        message.setFrom(new InternetAddress(userName));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
        message.setSubject(subject);
        MimeMultipart multipart = new MimeMultipart("mixed");
        MimeBodyPart attachmentPart = new MimeBodyPart();
        attachmentPart.attachFile(attachmentPath);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);

        // 发送消息
        Transport.send(message);

        System.out.println("Email sent successfully!");
    }

    public void sendEmail() {
        String host = "smtp.qq.com";
        String port = "587";
        String userName = "2643132195@qq.com";
        String password = "gsplqyfkqotteaai";

        // 填入收件人地址、邮件主题和邮件内容
        String toAddress = "liyang@lakala.com";
        String subject = "YDJ Web Test Logs";
        String emailBody = "------";
        String baseUrl = new BaseUrl().getBaseUrl();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String te = formatter.format(date);
        String attachmentPath = baseUrl + "/applog/" + te + "/" + te + ".log";

        try {
            new JavaMailUtil().sendEmail(host, port, userName, password, toAddress, subject, emailBody, attachmentPath);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

//    public static void main(String[] args) {
//        // 填入SMTP服务器的地址、端口、用户名、密码等信息
//        String host = "smtp.qq.com";
//        String port = "587";
//        String userName = "2643132195@qq.com";
//        String password = "gsplqyfkqotteaai";
//
//        // 填入收件人地址、邮件主题和邮件内容
//        String toAddress = "liyang@lakala.com";
//        String subject = "Test Email";
//        String emailBody = "This is a test email";
//        String attachmentPath = new BaseUrl().getBaseUrl();
//
//        try {
//            sendEmail(host, port, userName, password, toAddress, subject, emailBody, attachmentPath);
//        } catch (AddressException e) {
//            e.printStackTrace();
//        } catch (MessagingException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
}